home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / STRNCASE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-20  |  1.4 KB  |  51 lines

  1. /* derived from strncmp from Henry Spencer's stringlib */
  2. /* revised by ERS */
  3. /* i-changes by Alexander Lehmann */
  4. /* case changes by Kay Roemer */
  5.  
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. /*
  10.  * strncasecmp - compare at most n characters of string s1 to s2 without case
  11.  *           result is equivalent to strcmp(strlwr(s1),s2)),
  12.  *           but doesn't change anything
  13.  */
  14.  
  15. int                             /* <0 for <, 0 for ==, >0 for > */
  16. strncasecmp(scan1, scan2, n)
  17. register const char *scan1;
  18. register const char *scan2;
  19. size_t n;
  20. {
  21.         register char c1, c2;
  22.         register long count;
  23.  
  24.         if (!scan1) {
  25.                 return scan2 ? -1 : 0;
  26.         }
  27.         if (!scan2) return 1;
  28.         count = n;
  29.         do {
  30.                 c1 = *scan1++; c1=tolower(c1);
  31.                 c2 = *scan2++; c2=tolower(c2);
  32.         } while (--count >= 0 && c1 && c1 == c2);
  33.  
  34.         if (count < 0)
  35.                 return(0);
  36.  
  37.         /*
  38.          * The following case analysis is necessary so that characters
  39.          * which look negative collate low against normal characters but
  40.          * high against the end-of-string NUL.
  41.          */
  42.         if (c1 == c2)
  43.                 return(0);
  44.         else if (c1 == '\0')
  45.                 return(-1);
  46.         else if (c2 == '\0')
  47.                 return(1);
  48.         else
  49.                 return(c1 - c2);
  50. }
  51.